/************************************************************************************/
/*  Program name CAPAPIMP_PAY_SCHEDULE.sql                                          */
/*  Creater:  NIP Karen (PCCW - UniHub)                                             */
/*************   History   *************************                                */
/* 9 JAN, 2005   NIP Karen         First created                                    */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPAPIMP_PAY_SCHEDULE';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_AP_PAY_SCHEDULE;

	lv_SOB_Name		VARCHAR2(20);

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			lv_SOB_Name	:=	CAPBRIMP_COMMON.GetSOBShortNameOU(CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME));

			INSERT INTO AP_PAYMENT_SCHEDULES_ALL
			VALUES
			(
			CAPBRIMP_MAPPING.GetAPInvoiceIDMapping(C1.INVOICE_ID),	--INVOICE_ID,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.LAST_UPDATE_DATE,
			C1.PAYMENT_CROSS_RATE,
			C1.PAYMENT_NUM,
			C1.AMOUNT_REMAINING,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.CREATION_DATE,
			C1.DISCOUNT_DATE,
			C1.DUE_DATE,
			C1.FUTURE_PAY_DUE_DATE,
			C1.GROSS_AMOUNT,
			C1.HOLD_FLAG,
			C1.LAST_UPDATE_LOGIN,
			C1.PAYMENT_METHOD_LOOKUP_CODE,
			C1.PAYMENT_PRIORITY,
			C1.PAYMENT_STATUS_FLAG,
			C1.SECOND_DISCOUNT_DATE,
			C1.THIRD_DISCOUNT_DATE,
			CAPBRIMP_MAPPING.GetAPBatchIDMapping(C1.BATCH_ID),		--BATCH_ID,
			C1.DISCOUNT_AMOUNT_AVAILABLE,
			C1.SECOND_DISC_AMT_AVAILABLE,
			C1.THIRD_DISC_AMT_AVAILABLE,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE_CATEGORY,
			C1.DISCOUNT_AMOUNT_REMAINING,
			CAPBRIMP_COMMON.GetOrgID(C1.ORG_NAME),					--OrgID,
			C1.GLOBAL_ATTRIBUTE_CATEGORY,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			CAPBRIMP_COMMON.GetExtBankAccID(CAPBRIMP_COMMON.GetSOBID(lv_SOB_Name),C1.BANK_NAME,C1.BANK_ACC_NAME),		--EXTERNAL_BANK_ACCOUNT_ID,
			C1.INV_CURR_GROSS_AMOUNT,
			CAPBRIMP_MAPPING.GetAPInvSelCrtIDMapping(C1.CHECKRUN_ID)		--CHECKRUN_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
